﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Clocked
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // chatting
        }

        private int _lunchTimeRemaining;

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            // lunch
            _lunchTimeRemaining = int.Parse(textBox2.Text) * 60;
            Thread t = new Thread(Run);
            t.Start();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            // free time
        }

        private void Run()
        {
            while (true)
            {
                UpdateLunchTimer((int)TimeSpan.FromSeconds(_lunchTimeRemaining).TotalMinutes);
                Thread.Sleep(1000);
                _lunchTimeRemaining--;
            }
        }

        private void UpdateLunchTimer(int timeRemaining)
        {
            if (labelLunchRemaining.Dispatcher.CheckAccess())
            {
                labelLunchRemaining.Content = timeRemaining.ToString();
            }
            else
            {
                labelLunchRemaining.Dispatcher.Invoke(new Action<Label, int>(l => l.Content = timeRemaining.ToString()), timeRemaining);

            }
            labelLunchRemaining.Content = timeRemaining.ToString();
        }
    }
}
